package com.markedquilt.comment.controller.admin;

import com.markedquilt.comment.coomon.AdminPermission;
import com.markedquilt.comment.coomon.BussinessException;
import com.markedquilt.comment.coomon.CommonRes;
import com.markedquilt.comment.coomon.EmBussinessError;
import com.markedquilt.comment.service.CategoryService;
import com.markedquilt.comment.service.SellerService;
import com.markedquilt.comment.service.ShopService;
import com.markedquilt.comment.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import sun.misc.BASE64Encoder;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author ZhangKe
 * @Description
 * @date 2020/4/20 12:10.
 */
@CrossOrigin
@Controller("/admin/admin")
@RequestMapping("/admin/admin")
public class AdminController {

    @Value("${admin.email}")
    private String email;

    @Value("${admin.encryptPassword}")
    private String encryptPassword;

    public static final String CURRENT_ADMIN_SESSION = "currentAdminSession";

    @Autowired
    private HttpServletRequest httpServletRequest;

    @Autowired
    private UserService userService;

    @Autowired
    private SellerService sellerService;

    @Autowired
    private ShopService shopService;

    @Autowired
    private CategoryService categoryService;

    @RequestMapping("/index")
    @AdminPermission
    public ModelAndView index(){
        ModelAndView modelAndView=new ModelAndView("/admin/admin/index");
        modelAndView.addObject("userCount",userService.countAllUser());
        modelAndView.addObject("shopCount",shopService.CountAllShop());
        modelAndView.addObject("categoryCount",categoryService.CountAllCategory());
        modelAndView.addObject("sellerCount",sellerService.CountAllSeller());
        modelAndView.addObject("CONTROLLER_NAME","admin");
        modelAndView.addObject("ACTION_NAME","index");
        return modelAndView;
    }

    @RequestMapping("/loginpage")
    public ModelAndView login(){
        ModelAndView modelAndView=new ModelAndView("/admin/admin/login");
        return modelAndView;
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(@RequestParam(name = "email")String email,
                              @RequestParam(name = "password")String password) throws BussinessException, UnsupportedEncodingException, NoSuchAlgorithmException {
        if (StringUtils.isEmpty(email) || StringUtils.isEmpty(password)) {
            throw new BussinessException(EmBussinessError.PARAMETER_VALIDATION_ERROR,"用户名密码不能为空");
        }
        if (email.equals(this.email) && encodeByMd5(password).equals(this.encryptPassword)){
            httpServletRequest.getSession().setAttribute(CURRENT_ADMIN_SESSION,email);
            return "redirect:/admin/admin/index";
        }
        else
            throw new BussinessException(EmBussinessError.PARAMETER_VALIDATION_ERROR,"用户名密码错误");
    }

    private String encodeByMd5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest messageDigest=MessageDigest.getInstance("MD5");
        BASE64Encoder base64Encoder=new BASE64Encoder();
        return base64Encoder.encode(messageDigest.digest(str.getBytes("utf-8")));
    }
}
